package com.scisoft.scan.control.shelves;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ClearEditText;
import com.scisoft.scan.data.depot.AreaBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.data.depot.WarehouseBean;
import com.scisoft.scan.data.shelves.ShelvesInfoBean;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_Depot;
import com.scisoft.scan.ws.WebServiceHelper_Shelves;

import java.util.ArrayList;
import java.util.List;

public class ShelvesAddActivity extends Activity {

    private ImageView iv_Back; //返回按钮
    private TextView tv_save; //保存按钮
    //表单项
    private ClearEditText et_hjh;//货架号控件
    private Spinner spin_depot;//仓库列表控件
    private Spinner spin_area;//库区列表控件
    //仓库
    private List<String> depotList = new ArrayList<>();
    private List<WarehouseBean> depotBeanList = new ArrayList<>();
    private int selectedDepotNo = 0;
    //库区
    private List<String> areaList = new ArrayList<>();//库区字典
    private List<AreaBean> areaBeanList = new ArrayList<>();//库区字典
    private int selectedAreaNo = 0;
    private ShelvesInfoBean shelvesInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_shelves_add);
        //表单项
        et_hjh = findViewById(R.id.et_hjh);
        spin_depot = findViewById(R.id.spin_depot);

        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShelvesAddActivity.this.finish();
            }
        });
        //提交
        tv_save = findViewById(R.id.tv_save);
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GetData();
                SaveTask saveTask = new SaveTask(ShelvesAddActivity.this);
                saveTask.execute();
            }
        });

        CommonUtil.showPress(ShelvesAddActivity.this);
        QueryWarehouseTask queryTask = new QueryWarehouseTask(ShelvesAddActivity.this);
        queryTask.execute();
    }

    //获取数据,包括：仓库、货位、货架号
    private void GetData() {
        shelvesInfoBean = new ShelvesInfoBean();
        //货架号
        shelvesInfoBean.shelvesName = et_hjh.getUpperText().toString();
        //仓库
        if (selectedDepotNo >= 0 && selectedDepotNo < depotBeanList.size()) {
            shelvesInfoBean.depotId = depotBeanList.get(selectedDepotNo).depotId;
            shelvesInfoBean.depotName = depotBeanList.get(selectedDepotNo).depotName;
        } else {
            Toast.makeText(getApplicationContext(), "请选择仓库", Toast.LENGTH_LONG).show();
        }
        //库区
        if (selectedAreaNo >= 0 && selectedAreaNo < areaBeanList.size()) {
            shelvesInfoBean.areaId = areaBeanList.get(selectedAreaNo).areaId;
            shelvesInfoBean.areaName = areaBeanList.get(selectedAreaNo).areaName;
        } else {
            Toast.makeText(getApplicationContext(), "请选择库区", Toast.LENGTH_LONG).show();
        }
    }

    //绑定仓库字典数据
    private void SetSpanDepot(Context context) {
        spin_depot = findViewById(R.id.spin_depot);
        depotList.clear();
        for (int i = 0; i < depotBeanList.size(); i++) {
            depotList.add(depotBeanList.get(i).depotName);
        }
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.style_spinner, depotList);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_depot.setAdapter(adapter2);
        spin_depot.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedDepotNo = position;
                CommonUtil.showPress(ShelvesAddActivity.this);
                QueryAreaTask queryTask = new QueryAreaTask(ShelvesAddActivity.this);
                queryTask.execute();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    //绑定货区字典数据
    private void SetSpanArea(Context context) {
        spin_area = findViewById(R.id.spin_area);
        areaList.clear();
        for (int i = 0; i < areaBeanList.size(); i++) {
            areaList.add(areaBeanList.get(i).areaName);
        }
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.style_spinner, areaList);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_area.setAdapter(adapter2);
        spin_area.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedAreaNo = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    //查询仓库服务
    class QueryWarehouseTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        QueryWarehouseTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                //查询仓库字典项
                depotBeanList = WebServiceHelper_Depot.DEPOT_01();
                //根据当前货架的ID，设置当前仓库
                if (depotBeanList != null && depotBeanList.size() > 0) {
                    selectedDepotNo = 0;
                    areaBeanList = WebServiceHelper_Depot.DEPOT_12(depotBeanList.get(selectedDepotNo).depotId);
                }
                msg = "成功";

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                SetSpanDepot(mcontext);
                SetSpanArea(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //查询库区服务
    class QueryAreaTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        QueryAreaTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                if (depotBeanList != null && selectedDepotNo >= 0 && selectedDepotNo < depotBeanList.size()) {
                    areaBeanList = WebServiceHelper_Depot.DEPOT_12(depotBeanList.get(selectedDepotNo).depotId);
                    selectedAreaNo = 0;
                }
                msg = "成功";

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                SetSpanArea(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //保存数据服务
    class SaveTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        SaveTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                //查询列表数据
                msg = WebServiceHelper_Shelves.SHELVES_02(shelvesInfoBean);

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            if (msg.contains("成功")) {
                Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(ShelvesAddActivity.this, ShelvesActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }
}
